package it.andreaartuso.paletta.activity;

import it.andreaartuso.paletta.widget.ButtonLikeLinearLayout;
import it.andreaartuso.paletta.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {
	
	final ButtonLikeLinearLayout[] lines = new ButtonLikeLinearLayout[11];
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        this.init();
    }

    private void init(){

    	this.initLines();

    	Button btn_random = (Button)this.findViewById(R.id.btn_random);
    	btn_random.setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if(event.getAction() == MotionEvent.ACTION_DOWN){
		    		deselectAllOthers(11);
		    		Intent openPalettaActivityIntent = new Intent(".activity.PALETTAACTIVITY");
		    		Bundle b = new Bundle();
		    		int x = (int)(Math.random()*10);
		    		b.putInt("voto", x);
		    		openPalettaActivityIntent.putExtras(b);
					startActivity(openPalettaActivityIntent);
		    	}
		    	return true;
			}
		});
    	
		lines[0].setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) { 
				return selectAllBelow_Touch(0, event); }
		});
		lines[1].setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) { 
				return selectAllBelow_Touch(1, event); }
		});
		lines[2].setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) { 
				return selectAllBelow_Touch(2, event); }
		});
		lines[3].setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) { 
				return selectAllBelow_Touch(3, event); }
		});
		lines[4].setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) { 
				return selectAllBelow_Touch(4, event); }
		});
		lines[5].setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) { 
				return selectAllBelow_Touch(5, event); }
		});
		lines[6].setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) { 
				return selectAllBelow_Touch(6, event); }
		});
		lines[7].setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) { 
				return selectAllBelow_Touch(7, event); }
		});
		lines[8].setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) { 
				return selectAllBelow_Touch(8, event); }
		});
		lines[9].setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) { 
				return selectAllBelow_Touch(9, event); }
		});
		lines[10].setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) { 
				return selectAllBelow_Touch(10, event); }
		});
    }

    private void initLines(){
    	lines[0] = (ButtonLikeLinearLayout)this.findViewById(R.id.ll_lvl0);
    	lines[0].init(0);
		lines[1] = (ButtonLikeLinearLayout)this.findViewById(R.id.ll_lvl1);
		lines[1].init(1);
		lines[2] = (ButtonLikeLinearLayout)this.findViewById(R.id.ll_lvl2);
		lines[2].init(2);
		lines[3] = (ButtonLikeLinearLayout)this.findViewById(R.id.ll_lvl3);
		lines[3].init(3);
		lines[4] = (ButtonLikeLinearLayout)this.findViewById(R.id.ll_lvl4);
		lines[4].init(4);
		lines[5] = (ButtonLikeLinearLayout)this.findViewById(R.id.ll_lvl5);
		lines[5].init(5);
		lines[6] = (ButtonLikeLinearLayout)this.findViewById(R.id.ll_lvl6);
		lines[6].init(6);
		lines[7] = (ButtonLikeLinearLayout)this.findViewById(R.id.ll_lvl7);
		lines[7].init(7);
		lines[8] = (ButtonLikeLinearLayout)this.findViewById(R.id.ll_lvl8);
		lines[8].init(8);
		lines[9] = (ButtonLikeLinearLayout)this.findViewById(R.id.ll_lvl9);
		lines[9].init(9);
		lines[10] = (ButtonLikeLinearLayout)this.findViewById(R.id.ll_lvl10);
		lines[10].init(10);
    }

    private void deselectAllOthers(int level){
    	for(ButtonLikeLinearLayout btn : lines){
    		if(btn.getLevel()!=level){
    			btn.deselect();
    		}
    	}
    }
    
    private boolean selectAllBelow_Touch(int level, MotionEvent event){
    	if(event.getAction() == MotionEvent.ACTION_DOWN){
    		this.deselectAllOthers(level);
    		lines[level].select();
    		Intent openPalettaActivityIntent = new Intent(".activity.PALETTAACTIVITY");
    		Bundle b = new Bundle();
    		b.putInt("voto", level);
    		openPalettaActivityIntent.putExtras(b);
			startActivity(openPalettaActivityIntent);
    	}
    	return true;
    }

}